var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    wrap_one: true,
    ifTrue: true,
    Length: 6, //输入框个数
    isFocusOne: true, //聚焦
    ValueOne: '', //输入的内容
    // isFocusTwo: true,    //聚焦
    ValueTwo: '', //输入的内容
    ispassword: true, //是否密文显示 true为密文， false为明文。
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      type: options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  onFocusOne: function(e) {
    var that = this
    console.log(e.detail.value)
    var inputValue = e.detail.value.trim()
    that.setData({
      ValueOne: inputValue
    })
    if (inputValue.length >= 6) {
      that.setData({ wrap_one: false })
    }
  },

  onTapOne: function(e) {
    console.log(e.currentTarget.dataset.index)
    var that = this
    that.setData({
      isFocusOne: true
    })
  },
  onFocusTwo: function(e) {
    var that = this
    console.log(e.detail.value)
    var inputValue = e.detail.value.trim()
    that.setData({
      ValueTwo: inputValue
    })
    if (inputValue.length >= 6) {
      that.setData({ ifTrue: false })
    } else {
      that.setData({ ifTrue: true })
    }
  },

  onTapTwo: function(e) {
    console.log(e.currentTarget.dataset.index)
    var that = this
    that.setData({
      isFocusTwo: true
    })
  },

  formSubmit: function(e) {
    var that = this
    if (that.data.ValueOne == '' || that.data.ValueTwo == '') {
      wx.showToast({
        title: '各项信息必填',
        icon: 'none'
      })
      return false
    } else if (
      that.data.ValueOne.length != 6 ||
      that.data.ValueTwo.length != 6
    ) {
      wx.showToast({
        title: '密码长度有误',
        icon: 'none'
      })
      return false
    } else if (that.data.ValueOne != that.data.ValueTwo) {
      wx.showToast({
        title: '2次输入不一致',
        icon: 'none'
      })
      return false
    }
    if (
      that.data.ValueOne == that.data.ValueOne &&
      that.data.ValueOne.length == that.data.ValueTwo.length
    ) {
      that.setData({ ifTrue: true })
      wx.showLoading({ title: '提交中' })
      wx.request({
        method: 'POST',
        url: app.preUrl + 'setPay',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId,
          password: that.data.ValueOne
        },
        success: function(res) {
          console.log(res)
          wx.hideLoading()
          if (res.data.code == 100) {
            wx.showToast({
              title: '密码设置成功',
              icon: 'success',
              duration: 1000
            })
            console.log(that.data.type)
            if (that.data.type == 2) {
              //忘记密码//绑定银行卡设置密码
              setTimeout(function() {
                wx.navigateBack({
                  delta: 3
                })
              }, 1000)
              return
            }
            if (that.data.type == 1) {
              //新增密码
              setTimeout(function() {
                wx.navigateBack({
                  delta: 2
                })
              }, 1000)
              return
            }
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1000
            })
            that.setData({ ifTrue: false })
          }
        },
        fail: function(error) {
          console.log(error)
        }
      })
      // }else{
      //   wx.showModal({
      //     title: '2次密码必填且值相同长度为6',
      //     confirmColor: '#56BDE0',
      //     showCancel: false,
      //   })
    }
  }
})
